 /*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 /**
 * Project  : KindlePush
 * Package  : net.solosky.kindlepush.forum.provider
 * File     : DoubanForumProvider.java
 * Author   : solosky < solosky772@qq.com >
 * Created  : 2012-4-22
 * License  : Apache License 2.0 
 */
package net.solosky.kindlepush.forum.provider;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.solosky.kindlepush.forum.bean.Post;
import net.solosky.kindlepush.forum.bean.Topic;
import net.solosky.kindlepush.util.DateUtils;
import net.solosky.kindlepush.util.StringUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * 豆瓣小组接口的实现
 *
 * @author solosky <solosky772@qq.com>
 *
 */
public class DoubanForumProvider implements ForumProvider{

	@Override
	public Topic parseTopic(String url) throws IOException {
		Topic topic = new Topic();
		Document doc = Jsoup.connect(url).timeout(30*1000).get();
		String title = doc.select("#content>h1").text();
		String date = doc.select(".topic-doc>h3>span.color-green").text();
		String author = doc.select(".topic-doc>h3>span.pl20>a").text();
		topic.setTitle(title);
		topic.setAuthor(author);
		topic.setPostDate(DateUtils.parseDate(date));
		
		Elements e = doc.select(".paginator>a");
		List<String> urlList = new ArrayList<String>();
		if(e.size()>0){
			int maxPage = Integer.parseInt(e.get(e.size()-1).text());
			for(int i=0; i<maxPage; i++){
				urlList.add(url+"?start="+i*100);
			}
		}else{
			urlList.add(url);	//只有一页
		}
		topic.setPageUrls(urlList);
		topic.setMainUrl(url);
		return topic;
	}

	@Override
	public List<Post> parsePosts(String url) throws IOException {
		List<Post> posts = new ArrayList<Post>();
		Document doc = Jsoup.connect(url).timeout(30*1000).get();
		
		
		if(doc.select(".topic-content").size()>0){
			Post headPost = new Post();
			String date = doc.select(".topic-doc>h3>span.color-green").text();
			String author = doc.select(".topic-doc>h3>span.pl20>a").text();
			String content = StringUtils.cleanFormattedHtml(doc.select(".topic-content>p").html());
			
			headPost.setText(content);
			headPost.setAuthor(author);
			headPost.setDate(DateUtils.parseDate(date));
			
			posts.add(headPost);
		}
		
		Elements replies = doc.select(".topic-reply>li");
		for(int i=0; i<replies.size(); i++){
			Element reply = replies.get(i);
			Post post = new Post();
			
			String postInfo = reply.select("h4").text();
			String postDate = postInfo.substring(0, 19);
			String author = reply.select("h4>a").text();
			String content = StringUtils.cleanFormattedHtml(reply.select(".reply-doc>p").html());
			
			post.setDate(DateUtils.parseDate(postDate));
			post.setAuthor(author);
			post.setText(content);
			
			posts.add(post);
		}
		return posts;
	}
	
	public static void main(String[] args) throws IOException {
		ForumProvider provider = new DoubanForumProvider();
		Topic topic = provider.parseTopic("http://www.douban.com/group/topic/27866438/");
		for(String url: topic.getPageUrls()){
			System.out.println("FETCH POSTS:"+url);
			List<Post> posts = provider.parsePosts(url);
			for(Post post:posts){
				System.out.println(post.getDate()+"=>"+post.getAuthor());
				System.out.println(post.getText());
				System.out.println("-------------------------------------------------------------------------");
			}
		}
	
		//System.out.println(topic.getPageUrlList());
		//System.out.println(topic.getTitle());
	}

}
